package com.cmcc.linlaker.ui.map;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.ui.query.NewProjectActivity;
import com.cmcc.linlaker.utils.BarUtils;

public class MapActivity extends AppCompatActivity implements View.OnClickListener {

    private MapView mMapView;
    private AMap aMap;

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mMapView != null) {
            mMapView.onSaveInstanceState(outState);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_map);

        mMapView = findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        BarUtils.initStatusBar(this);

        initView();
    }

    public void back(View view) {
        finish();
    }


    public void initView() {
        initMap();
    }

    /**
     * 初始化地图
     */
    private void initMap() {
        if (aMap == null) {
            aMap = mMapView.getMap();
            //设置小蓝点
            setUpMap();
        } else {
            aMap.clear();
            aMap = mMapView.getMap();
            //设置小蓝点
            setUpMap();
        }
        aMap.getUiSettings().setZoomControlsEnabled(false);//去掉高德地图右下角隐藏的缩放按钮
        aMap.getUiSettings().setZoomGesturesEnabled(true);
    }


    /**
     * 设置地图小蓝点
     */
    private void setUpMap() {
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(NewProjectActivity.latitude, NewProjectActivity.longitude), 10));//初始化中心位置 和缩放比例
        // 自定义系统定位小蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.location_icon));// 设置小蓝点的图标
        myLocationStyle.strokeColor(Color.argb(66, 34, 119, 185));// 设置圆形的边框颜色
        myLocationStyle.radiusFillColor(Color.argb(66, 34, 119, 185));// 设置圆形的填充颜色
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);

        aMap.setMyLocationStyle(myLocationStyle);
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        aMap.setMapType(AMap.MAP_TYPE_NORMAL);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //重新绘制加载地图
        mMapView.onResume();
    }

}
